package com.tarena.lbs.basic.web.repository.impl;

import com.tarena.lbs.basic.web.mapper.AreaMapper;
import com.tarena.lbs.basic.web.repository.AreaRepository;
import com.tarena.lbs.common.basic.converter.AreaConverter;
import com.tarena.lbs.pojo.basic.bo.AreaBO;
import com.tarena.lbs.pojo.basic.dos.AreaDO;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.stereotype.Repository;

@Repository
public class AreaRepositoryImpl implements AreaRepository {


    @Resource
    private AreaMapper areaMapper;

    @Resource
    private AreaConverter areaConverter;

    /**
     * 功能描述: 查询区域树
     */
    @Override
    public Map<Long, List<AreaBO>> queryAreaData() {
        List<AreaDO> areaList =  areaMapper.queryList();
        List<AreaBO> areaBoList = areaConverter.doList2boList(areaList);
        return areaBoList.stream().collect(Collectors.groupingBy(AreaBO::getParentId));
    }

    /**
     * 功能描述: 查询一级省区域
     */
    @Override public List<AreaBO> queryProvinceList() {
        List<AreaDO> areaDOList = areaMapper.queryProvinceList();
        return areaConverter.doList2boList(areaDOList);
    }

    /**
     * 功能描述: 根据上一级查询下级数据
     */
    @Override
    public List<AreaBO> findByParentId(Long parentId) {
        List<AreaDO> areaDOList = areaMapper.findByParentId(parentId);
        return areaConverter.doList2boList(areaDOList);
    }

    @Override
    public AreaBO findById(Long id) {
        AreaDO areaDO = areaMapper.selectByPrimaryKey(id);
        return areaConverter.do2bo(areaDO);
    }
}
